package com.enmonster.executor;

import com.enmonster.jdbc.JdbcUtils;
import com.enmonster.resolve.original.ModelOriginal;
import com.enmonster.resolve.original.ResultOriginal;
import com.enmonster.result.Result;
import com.enmonster.result.ResultType;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wuxi
 * @create 2019/4/11 下午2:23
 */
public class SelectExecutor implements BaseExecutor{

    @Override
    public Object executor(ModelOriginal modelOriginal, Method method , ResultOriginal resultOriginal) throws Exception{
        ResultSet res = JdbcUtils.query(modelOriginal.getSql(), modelOriginal.getParamxEcuteList());
        // 判断是否存在值
        if (!res.next()) {
            return null;
        }
        // 下标往上移动移位
        res.previous();
        Type resultType = ResultType.resolveReturnType(method);
        Result result = new Result();
        Object obj = result.getResult(resultType, res, resultOriginal.getResultParamMap ());
        return obj;
    }
}
